<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/27
 * Time: 上午6:47
 */

namespace App\Services;


use App\Models\Apart;
use App\Models\Fee;
use App\Models\User;

class FeeService extends Service
{

    public static function getFeeList($limit, $offset, $order = 'desc', $option = [])
    {
        $query = Fee::where('status', '>', 0);
        if (count($option) && count($option['query'])) {
            foreach ($option['query'] as $search) {
                if ($search['key'] == 'apart_id') {
                    $query = $query->whereIn($search['key'], $search['value']);
                } else {
                    $query = $query->where($search['key'], $search['operator'], $search['value']);
                }
            }
        }
        $total = $query->count();
        $fees = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($fees as $fee) {
            $apart = Apart::where('id', $fee->apart_id)->first();
            $user = User::where('id', $fee->user_id)->first();
            $data[] = [
                'id' => $fee->id,
                'name' => $apart->name,
                'rent_user_number' => $user->number,
                'rent_user_name' => $user->realname . '（' . $user->number . '）',
                'fee_time' => local_date($fee->start_at) . '~' . local_date($fee->end_at),
                'num' => $fee->num,
                'fee' => $fee->fee,
                'last_process' => $fee->last_process,
                'process' => $fee->process,
                'pay_type' => $fee->pay_type,
                'pay_at' => $fee->pay_at,
                'pay_order' => $fee->pay_order,
                'status' => $fee->status,
                'remark' => $fee->remark
            ];
        }
        return [
            'total' => $total,
            'rows' => $data,
            'limit' => $limit,
            'offset' => $offset,
            'page' => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getFeeDetail($id)
    {
        $fee = Fee::where('id', $id)->first();

        if (!$fee) {
            return [];
        }

        $apart = Apart::where('id', $fee->apart_id)->first();
        $user = User::where('id', $fee->user_id)->first();

        return [
            'data' => [
                'id' => $fee->id,
                'name' => $apart->name,
                'rent_user_number' => $user->number,
                'rent_user_name' => $user->realname . '（' . $user->number . '）',
                'fee_time' => $fee->start_at . '~' . $fee->end_at,
                'num' => $fee->num,
                'fee' => $fee->fee,
                'pay_type' => $fee->pay_type,
                'pay_at' => $fee->pay_at,
                'pay_order' => $fee->pay_order,
                'remark' => $fee->remark
            ]
        ];
    }

    public static function getFeeLast($type, $apartId, $userId)
    {
        $fee = Fee::where('type', $type)->where('apart_id', $apartId)->where('user_id', $userId)->orderBy('id', 'desc')->first();

        if (!$fee) {
            return [];
        }

        $apart = Apart::where('id', $fee->apart_id)->first();
        $user = User::where('id', $fee->user_id)->first();

        return [
            'data' => [
                'id' => $fee->id,
                'apart_id' => $fee->apart_id,
                'apart_name' => $apart->name,
                'user_id' => $fee->user_idr,
                'realname' => $user->realname,
                'start_at' => local_date($fee->start_at),
                'end_at' => local_date($fee->end_at),
                'start_end_time' => local_date($fee->start_at) . ' ~ ' . local_date($fee->end_at),
                'past_process' => $fee->past_process,
                'process' => $fee->process,
                'price' => $fee->price,
                'num' => $fee->num,
                'fee' => $fee->fee,
                'pay_type' => $fee->pay_type,
                'pay_at' => $fee->pay_at,
                'pay_order' => $fee->pay_order,
                'remark' => $fee->remark
            ]
        ];
    }

    public static function changePayType($data)
    {
        $fee = Fee::where('id', $data['id'])->first();
        $fee->pay_type = $data['pay_type'];
        if ($data['pay_type'] == 2 && array_key_exists('pay_order', $data)) {
            $fee->pay_order = $data['pay_order'];
            $fee->pay_at = date('Y-m-d H:i:s', time());
        }
        if (array_key_exists('status', $data) && $data['status'] == 2) {
            $fee->status = $data['status'];
        }
        $fee->remark .= array_key_exists('remark', $data) ? $data['remark'] : '';
        $fee->updated_at = date('Y-m-d H:i:s', time());
        $fee->save();
    }

    public static function saveFee($data)
    {
        $fee = new Fee();
        if (array_key_exists('id', $data) && $data['id']) {
            $fee = Fee::where('id', $data['id'])->first();
        } else {
            $fee->created_at = date('Y-m-d H:i:s', time());
        }
        foreach ($data as $key => $value) {
            $fee->$key = $value;
        }
        $fee->num = $data['process'] - $data['last_process'];
        $fee->fee = $data['price'] * $fee->num;

        $fee->updated_at = date('Y-m-d H:i:s', time());
        $fee->save();
    }

    public static function deleteFee($id)
    {
        $fee = Fee::where('id', $id)->first();
        $fee->status = 0;
        $fee->updated_at = date('Y-m-d H:i:s', time());
        $fee->save();
    }
}